import {BaseService} from 'egg-cool-service';
import * as _ from 'lodash';

/**
 * 商品-分类
 */
export default class GoodsCateService extends BaseService {

    /**
     * 新增
     * @param param
     */
    async add (param) {
        // const exists = await this.getRepo().goods.Cate.findOne({ name: param.name });
        // console.log('exists',exists)
        // if (!_.isEmpty(exists)) {
        //     throw new Error('分类已经存在~');
        // }
        // param.password = md5('123456'); // 默认密码  建议未改密码不能登陆
        // console.log(this.getRepo().goods)
        await this.getRepo ().goods.Cate.save (param);
    }

    // async list(query){
    //     const exists = await this.getRepo().goods.Cate.find();
    //     const {platform} = query
    //     console.log('platform',platform)
    //     // console.log('param',param)
    //     if(platform == 'weapp'){
    //         return exists.filter(item=>item.id != 1)
    //     }
    //     return  exists
    // }

    /**
     * 分类排序
     * @param params
     */
    async order (params) {
        for (const e of params) {
            await this.getRepo ().goods.Cate.update (e.id , e);
        }
    }

    /**
     * 获得分类菜单
     */
    async list () {
        const list = await this.nativeQuery (`
        SELECT
            *
            FROM
        goods_cate a
        WHERE
        1 = 1
        ORDER BY
        a.orderNum ASC`);
        // if (!_.isEmpty(list)) {
        //     list.forEach(e => {
        //         const parentMenu = list.filter(m => {
        //             if (e.parentId === m.id) {
        //                 return m.name;
        //             }
        //         });
        //         if (!_.isEmpty(parentMenu)) {
        //             e.parentName = parentMenu[0].name;
        //         }
        //         e.bannerImage = e.bannerImage?{url:e.bannerImage}:{}
        //     });
        // }
        return list;
    }

    async info (id , entity) {
        let item = await entity.findOne ({id});
        // item.bannerImage = item.bannerImage?{url:item.bannerImage}:{url:''}
        return item;
    }

}
